package com.kx.yooli.kx.wechat.mapper.user;

import com.kx.yooli.kx.wechat.po.user.UserBindAuthInfo;
import com.kx.yooli.kx.wechat.po.user.UserInfo;
import com.kx.yooli.kx.wechat.po.user.UserInfoSso;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserInfoSsoMapper extends BaseMapper<UserInfoSso> {
    default UserInfoSso selectByPhoneDefault(String phone){
        return selectOneWithRelationsByQuery(QueryWrapper.create()
                .select()
                .from("user_info_sso")
                .where("mobile=" + "'"+phone+"'"));
    }
    default UserInfoSso selectByCertNoDefault(String certNo){
        return selectOneWithRelationsByQuery(QueryWrapper.create()
                .select()
                .from(UserInfoSso.class)
                .where(UserInfoSso::getCertNo).eq(certNo));
    }
    default List<UserInfoSso> selectPhoneOrCertNo(String phone,String certNo){
        return selectListWithRelationsByQuery(QueryWrapper.create()
                .select("id, mobile, cert_no as certNo, 'sso' as name")
                .from(UserInfoSso.class)
                .where(UserInfoSso::getCertNo).eq(certNo).or(UserInfoSso::getMobile).eq(phone)
                .unionAll(
                        QueryWrapper.create().select("id, mobile, cert_no as certNo, 'info' as name").from(UserInfo.class)
                                .where(UserInfo::getCertNo)
                                .eq(certNo)
                                .or(UserInfo::getMobile)
                                .eq(phone)
                )
        );
    }

    default List<UserInfoSso> selectIsExistCertNo(String mobile){
        return selectListWithRelationsByQuery(QueryWrapper.create()
                .select(UserInfoSso::getId, UserInfoSso::getMobile, UserInfoSso::getCertNo)
                .from(UserInfoSso.class)
                .where(UserInfoSso::getMobile).eq(mobile)
                .unionAll(
                        QueryWrapper.create().select(UserInfo::getId, UserInfo::getMobile, UserInfo::getCertNo).from(UserInfo.class)
                                .where(UserInfo::getMobile)
                                .eq(mobile)
                )
        );
    }

    default boolean updateMobileByCertNo(UserInfoSso userInfoSso){
        return UpdateChain.of(UserInfoSso.class)
                .setRaw(UserInfoSso::getMobile,userInfoSso.getMobile())
                .where(UserInfoSso::getCertNo).eq(userInfoSso.getCertNo()).update();
    }


}
